<?php

namespace app\controller;

use app\Request;
use app\service\MenuService;

class MenuController
{
    public function getList()
    {
        $result = MenuService::getInstance()->getMenuList();
        return jsonResponse($result);
    }


    public function getMenusByPermission(Request $request)
    {
        $user = $request->getUserInfo();
        $result = MenuService::getInstance()->getMenusByPermission($user["menu_ids"]);
        return jsonResponse($result);
    }

    public function create(Request $request)
    {
        $name = $request->post('name');
        $pid = $request->post('pid');
        $icon = $request->post('icon');
        $url = $request->post('url');

        $result = MenuService::getInstance()->createMenu($name, $pid, $icon, $url);
        return jsonResponse($result);
    }

    public function update(Request $request)
    {
        $id = $request->post('id');
        $name = $request->post('name');
        $pid = $request->post('pid');
        $icon = $request->post('icon');
        $url = $request->post('url');

        $result = MenuService::getInstance()->updateMenu($id, $name, $pid, $icon, $url);
        return jsonResponse($result);
    }

    public function delete(Request $request)
    {
        $id = $request->post('id');
        $result = MenuService::getInstance()->deleteMenu($id);
        return jsonResponse($result);
    }
}